package com.mjf.scala.chapter07

/**
 * 复杂版本 WordCount
 */
object Scala16_TestWordCount {
  def main(args: Array[String]): Unit = {

    // ("Hello Scala Spark World ", 4) 表示 4次 "Hello Scala Spark World "
    val tupleList = List(("Hello Scala Spark World ", 4), ("Hello Scala Spark", 3), ("Hello Scala", 2), ("Hello", 1))

    // 将元组（字符串，次数）  进行转换为大的字符串
    val newList: List[String] = tupleList.map(kv => (kv._1.trim + " ") * kv._2)
    println(newList)

    // 扁平映射
    val flagList: List[String] = newList.flatMap(_.split(" "))
    println(flagList)

    println(flagList.groupBy(elem => elem).toList.map(
      kv => (kv._1, kv._2.size)
    ).sortWith(_._2 > _._2).take(3))

  }
}
